from sklearn.model_selection import train_test_split
from sklearn.datasets import load_iris
from sklearn.neighbors import KNeighborsClassifier
 
# 加载鸢尾花数据集
iris = load_iris()
X = iris.data
y = iris.target
print(X)
# 划分数据集为训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
 
# 创建并训练KNN模型
knn = KNeighborsClassifier(n_neighbors=5)
knn.fit(X_train, y_train)
 
# 在测试集上评估模型
print(knn.score(X_test, y_test))